/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.run;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;

public class WorkbenchInfo {

	private static String specialChars[][] = {
			{ "&#128;", "&#x80;", "€", "&#8364;" },
			{ "&#130;", "&#x82;", "‚", "&#8218;" },
			{ "&#131;", "&#x83;", "ƒ", "&#402;" },
			{ "&#132;", "&#x84;", "„", "&#8222;" },
			{ "&#133;", "&#x85;", "…", "&#8230;" },
			{ "&#134;", "&#x86;", "†", "&#8224;" },
			{ "&#135;", "&#x87;", "‡", "&#8225;" },
			{ "&#136;", "&#x88;", "ˆ", "&#710;" },
			{ "&#137;", "&#x89;", "‰", "&#8240;" },
			{ "&#138;", "&#x8A;", "Š", "&#352;" },
			{ "&#139;", "&#x8B;", "‹", "&#8249;" },
			{ "&#140;", "&#x8C;", "Œ", "&#338;" },
			{ "&#142;", "&#x8E;", "Ž", "&#381;" },
			{ "&#145;", "&#x91;", "‘", "&#8216;" },
			{ "&#146;", "&#x92;", "’", "&#8217;" },
			{ "&#147;", "&#x93;", "“", "&#8220;" },
			{ "&#148;", "&#x94;", "”", "&#8221;" },
			{ "&#149;", "&#x95;", "•", "&#8226;" },
			{ "&#150;", "&#x96;", "–", "&#8211;" },
			{ "&#151;", "&#x97;", "—", "&#8212;" },
			{ "&#152;", "&#x98;", "˜", "&#732;" },
			{ "&#153;", "&#x99;", "™", "&#8482;" },
			{ "&#154;", "&#x9A;", "š", "&#353;" },
			{ "&#155;", "&#x9B;", "›", "&#8250;" },
			{ "&#156;", "&#x9C;", "œ", "&#339;" },
			{ "&#158;", "&#x9E;", "ž", "&#382;" },
			{ "&#159;", "&#x9F;", "Ÿ", "&#376;" } };

	//
	// public static IEditorPart findEditor(IFile file) {
	// IEditorReference[] editors = PlatformUI.getWorkbench()
	// .getActiveWorkbenchWindow().getActivePage()
	// .getEditorReferences();
	// ;
	// for (int i = 0; i < editors.length; i++) {
	// IEditorPart part = (IEditorPart) editors[i].getPart(false);
	// if (part != null) {
	// IEditorInput input = part.getEditorInput();
	// if (input instanceof FileEditorInput
	// && ((FileEditorInput) input).getFile().equals(file))
	// return part;
	// }
	// }
	// return null;
	// }

	public static void focusEditor() {
		IWorkbenchPage page = getActivePage();
		if (page != null) {
			IEditorPart editor = page.getActiveEditor();
			if (editor != null) {
				editor.setFocus();
			}
		}

	}

	/*
	 * public static IEditorPart getActiveEditor() { IWorkbenchPage page =
	 * getActivePage(); if (page != null) { return page.getActiveEditor(); }
	 * return null; }
	 * 
	 * public static String getActiveEditorText() { IDocument doc =
	 * getDocument(); if (doc != null) { String str = doc.get(); str =
	 * transformSpecialChars(str); return str; } return null; }
	 */
	public static IWorkbenchPage getActivePage() {
		IWorkbenchWindow window = getActiveWindow();
		if (window != null) {
			return window.getActivePage();
		}
		return null;
	}

	public static IWorkbenchWindow getActiveWindow() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		if (workbench != null) {
			return workbench.getActiveWorkbenchWindow();
		}
		return null;
	}

	public static ISelection getEditorSelection(ITextEditor textEditor) {
		return textEditor.getEditorSite().getSelectionProvider().getSelection();
	}

	public static String getEditorText(IEditorPart editorPart) {
		IDocument doc = getIDocument(editorPart);
		if (doc != null) {
			String str = doc.get();
			str = transformSpecialChars(str);
			return str;
		}
		return null;
	}

	// aktuelle IDocument
	public static IDocument getIDocument(IEditorPart editorPart) {
		ITextEditor editor = getTextEditor(editorPart);
		if (editor != null)
			return editor.getDocumentProvider().getDocument(
					editor.getEditorInput());
		return null;
	}

	public static ITextEditor getTextEditor(IEditorPart editorPart) {
		if (editorPart == null)
			return null;

		ITextEditor textEditor = null;
		if (editorPart instanceof ITextEditor) {
			textEditor = (ITextEditor) editorPart;
		} else
			textEditor = (ITextEditor) editorPart.getAdapter(ITextEditor.class);
		return textEditor;
	}

	// ITextSelection
	public static ITextSelection getTextSelection(IEditorPart editor) {
		IEditorSite site = editor.getEditorSite();
		ISelection sel = site.getSelectionProvider().getSelection();
		if (sel instanceof ITextSelection) {
			return (ITextSelection) sel;
		}
		return null;
	}

	// Map some latin1 characters unto their unicode code point using the array
	// from above
	public static String transformSpecialChars(String tempXML) {
		String pattern;
		pattern = specialChars[0][0] + "|" + specialChars[0][1] + "|"
				+ specialChars[0][2];
		for (int i = 1; i < specialChars.length; ++i) {
			pattern += "|" + specialChars[i][0] + "|" + specialChars[i][1]
					+ "|" + specialChars[i][2];
		}

		Pattern p = Pattern.compile(pattern);
		Matcher matcher = p.matcher(tempXML);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			for (int i = 0; i < specialChars.length; ++i) {
				if (matcher.group().equals(specialChars[i][0])
						|| matcher.group().equals(specialChars[i][1])
						|| matcher.group().equals(specialChars[i][2])) {
					matcher.appendReplacement(sb, specialChars[i][3]);
					break;
				}
			}
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
}
